package wgdm

import (
	//"errors"
	//"net"
	//"time"
	log "ac-common-go/glog"
	"ac-julink/rpcagent"
	"ac-julink/tool/config"
	//"ac-julink/tool/util"

	//"git.apache.org/thrift.git/lib/go/thrift"

	"ac"
	"fmt"
)



//var thriftPoolManager *rpcagent.ThriftPoolManager
var httpProxy *rpcagent.Proxy

//func dial(ip, port string, connTimeout time.Duration) (*rpcagent.IdleClient, error) {
//	tSocket, err := thrift.NewTSocketTimeout(net.JoinHostPort(ip, port), connTimeout)
//	if err != nil {
//		log.Errorf("connect to wgdm error")
//		return nil, err
//	}
//	if err := tSocket.Open(); err != nil {
//		return nil, err
//	}
//	transport := thrift.NewStreamTransportFactory(tSocket, tSocket, true).GetTransport(tSocket)
//	//	transport := thrift.NewTTransportFactory().GetTransport(tSocket)
//	//	transport := thrift.NewTBufferedTransportFactory(1024).GetTransport(tSocket)
//	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
//
//	client := NewWGDMServiceClientFactory(transport, protocolFactory)
//	return &rpcagent.IdleClient{Socket: tSocket, Client: client, CreateTime: time.Now()}, nil
//}
//func close(c *rpcagent.IdleClient) error {
//	err := c.Socket.Close()
//	return err
//}
func InitTriftPool() {

	ip := config.GetConfigValue("WGNS", "http_dm_ip")
	if ip == "" {
		log.Errorf("rpc_dm_ip is not valid!!")
		return
	}
	httpPort := config.GetConfigValue("WGNS", "http_dm_port")
	if httpPort == "" {
		log.Errorf("http_dm_port is not valid!!")
		return
	}
	maxConnsPerHost, ok := config.GetConfigIntValue("WGNS", "http_dm_maxconnsPerHost")
	if !ok {
		maxConnsPerHost = rpcagent.DEFAULTMAXCONNSPERHOST
	}

	respHeaderTimeout, ok := config.GetConfigIntValue("WGNS", "http_dm_response_header_timeout")
	if !ok {
		respHeaderTimeout = rpcagent.DEFAULTRESPHEASERTIMEOUT
	}
	idleTimeout, ok := config.GetConfigIntValue("WGNS", "http_dm_idletimeout")
	if !ok {
		idleTimeout = rpcagent.DEFAULTIDLETIME
	}
	retryNum, ok := config.GetConfigIntValue("WGNS", "http_dm_retrynum")
	if !ok {
		retryNum = rpcagent.DEFAULTRETRYNUM
	}

	httpProxy = rpcagent.NewProxy(ip, httpPort, maxConnsPerHost, respHeaderTimeout, idleTimeout, retryNum,false)
	log.Infof("DM http pool init success!")
}

//func GetWifiHomeInfo(token *Token, wifiId string) (r *ResultHomeInfo, err error) {
//	log.Infof("Rpc call GetWifiHomeInfo wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 2)
//	args[0] = token
//	args[1] = wifiId
//	result, err := thriftPoolManager.AskRpc("GetWifiHomeInfo", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("GetWifiHomeInfo result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultHomeInfo)
//	if !ok {
//		return nil, errors.New("interface to ResultHomeInfo failed")
//	}
//	return b, nil
//}

type AddWifiDeviceReq struct {
	WifiId     string `json:"wifiId"`
	DeviceId  string `json:"deviceId"`
}

func AddWifiDevice(token *Token, wifiId string, deviceId string) (r *HttpCommonResp, err error) {
	log.Infof("Http call AddWifiDevice wifiId[%s]", wifiId)

	reqctx := ac.NewZContext("", "", 0, 0,"", "zc-julink")
	req := AddWifiDeviceReq{
		WifiId:   wifiId,
		DeviceId:   deviceId,
	}
	var resp HttpCommonResp
	if err := httpProxy.DirectCall(reqctx, "add_wifi_device", "POST", "", &req, &resp); err != nil {
		log.Warningf("call add_wifi_device: err[%v]", err)
		return nil, err
	}
	return &resp, nil
}

//func AddWifiDevice(token *Token, wifiId string, deviceId string) (r *ResultCommon, err error) {
//	log.Infof("Rpc call AddWifiDevice wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 3)
//	args[0] = token
//	args[1] = wifiId
//	args[2] = deviceId
//	result, err := thriftPoolManager.AskRpc("AddWifiDevice", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("AddWifiDevice result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultCommon)
//	if !ok {
//		return nil, errors.New("interface to ResultCommon failed")
//	}
//	return b, nil
//}


func DeleteWifiDevice(token *Token, wifiId string, deviceId string) (r *HttpCommonResp, err error) {
	log.Infof("Http call DeleteWifiDevice wifiId[%s]", wifiId)
	reqctx := ac.NewZContext("", "", 0, 0,"", "zc-julink")
	req := AddWifiDeviceReq{
		WifiId:   wifiId,
		DeviceId:   deviceId,
	}
	var resp HttpCommonResp
	if err := httpProxy.DirectCall(reqctx, "delete_wifi_device", "POST", "", &req, &resp); err != nil {
		log.Warningf("call delete_wifi_device: err[%v]", err)
		return nil, err
	}
	return &resp, nil
}

//func DeleteWifiDevice(token *Token, wifiId string, deviceId string) (r *ResultCommon, err error) {
//	log.Infof("Rpc call DeleteWifiDevice wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 3)
//	args[0] = token
//	args[1] = wifiId
//	args[2] = deviceId
//	result, err := thriftPoolManager.AskRpc("DeleteWifiDevice", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("DeleteWifiDevice result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultCommon)
//	if !ok {
//		return nil, errors.New("interface to ResultCommon failed")
//	}
//	return b, nil
//}

//func GetHomeDeviceListByWifiId(wifiId string) (r *ResultHomeDeviceList, err error) {
//	log.Infof("Rpc call GetHomeDeviceListByWifiId wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 1)
//	args[0] = wifiId
//	result, err := thriftPoolManager.AskRpc("GetHomeDeviceListByWifiId", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("GetHomeDeviceListByWifiId result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultHomeDeviceList)
//	if !ok {
//		return nil, errors.New("interface to ResultHomeDeviceList failed")
//	}
//	return b, nil
//}

type SaveDeviceExtendsValueReq struct {
	PlatformId          string `json:"platformId"`
	CustomerId     string `json:"customerId"`
	LanguageId   string `json:"languageId"`
	WifiId     string `json:"wifiId"`
	DeviceId  string `json:"deviceId"`
	ExtKey  string `json:"extKey"`
	ExtValue  string `json:"extValue"`
	ExtDispValue  string `json:"extDispValue"`
	ExtInfoId  string `json:"extInfoId"`
}

type HttpCommonResp struct {
	ResultCode int32  `json:"resultCode"`
}

func SaveDeviceExtendsValue(token *Token, wifiId string, deviceId string, extKey string, extValue string, extInfoDisplayValue string, extInfoId int64, extInfoParentId int64) (r *HttpCommonResp, err error) {
	log.Infof("Http Rpc call SaveDeviceExtendsValue wifiId[%s]", wifiId)
	reqctx := ac.NewZContext("", "", 0, 0,"", "zc-julink")
	req := SaveDeviceExtendsValueReq{
		WifiId:   wifiId,
		DeviceId:   deviceId,
		LanguageId:  "1", //1 english
		ExtKey: extKey,
		ExtValue:extValue,
		ExtDispValue: extInfoDisplayValue,
		ExtInfoId : fmt.Sprintf("%d",extInfoId),
	}
	var resp HttpCommonResp
	if err := httpProxy.DirectCall(reqctx, "save_device_extends_value", "POST", "", &req, &resp); err != nil {
		log.Warningf("call save_device_extends_value: err[%v]", err)
		return nil, err
	}
	return &resp, nil
}

//func SaveDeviceExtendsValue(token *Token, wifiId string, deviceId string, extKey string, extValue string, extInfoDisplayValue string, extInfoId int64, extInfoParentId int64) (r *ResultCommon, err error) {
//	log.Infof("Rpc call SaveDeviceExtendsValue wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 8)
//	args[0] = token
//	args[1] = wifiId
//	args[2] = deviceId
//	args[3] = extKey
//	args[4] = extValue
//	args[5] = extInfoDisplayValue
//	args[6] = extInfoId
//	args[7] = extInfoParentId
//
//	result, err := thriftPoolManager.AskRpc("SaveDeviceExtendsValue", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("SaveDeviceExtendsValue result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultCommon)
//	if !ok {
//		return nil, errors.New("interface to ResultCommon failed")
//	}
//	return b, nil
//}


type DeviceResetReq struct {
	WifiId     string `json:"wifiId"`
	ResetFlag  string `json:"resetFlag"`
}

func DeviceReset(wifiId string, resetFlag int32) (r *HttpCommonResp, err error) {
	log.Infof("Http call DeviceReset wifiId[%s]", wifiId)
	reqctx := ac.NewZContext("", "", 0, 0,"", "zc-julink")
	req := DeviceResetReq{
		WifiId:   wifiId,
		ResetFlag:   fmt.Sprintf("%d", resetFlag),
	}
	var resp HttpCommonResp
	if err := httpProxy.DirectCall(reqctx, "device_reset", "POST", "", &req, &resp); err != nil {
		log.Warningf("call device_reset: err[%v]", err)
		return nil, err
	}
	return &resp, nil
}

//func DeviceReset(wifiId string, resetFlag int32) (r *ResultCommon, err error) {
//	log.Infof("Rpc call DeviceReset wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 2)
//	args[0] = wifiId
//	args[1] = resetFlag
//	result, err := thriftPoolManager.AskRpc("DeviceReset", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("DeviceReset result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultCommon)
//	if !ok {
//		return nil, errors.New("interface to ResultCommon failed")
//	}
//	return b, nil
//}

//func GetWifiDeviceList(token *Token, wifiId string) (r *ResultWifiDeviceList, err error) {
//	log.Infof("Rpc call GetWifiDeviceList wifiId[%s]", wifiId)
//	if thriftPoolManager == nil {
//		return nil, errors.New("wgdm pool manager is nil")
//	}
//	args := make([]interface{}, 2)
//	args[0] = token
//	args[1] = wifiId
//	result, err := thriftPoolManager.AskRpc("GetWifiDeviceList", args)
//	if err != nil {
//		return nil, err
//	}
//	if result == nil {
//		log.Errorf("GetWifiDeviceList result is nil, param:%s", wifiId)
//		return nil, errors.New("result is nil")
//	}
//	b, ok := result.(*ResultWifiDeviceList)
//	if !ok {
//		return nil, errors.New("interface to ResultWifiDeviceList failed")
//	}
//	return b, nil
//}
